{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基本逻辑\n",
    "\n",
    "选股涉及两个方面，一是公司分析，包括财务状况、发展潜力和成长性等，这方面是俗称的基本面分析，可以参考的资料已经汗牛充栋；二是股票分析。\n",
    "股票分析主要回答三个问题：  \n",
    "（1）如何判断一只股票有投资价值？  \n",
    "（2）如何从股票池中选出符合自己认为有价值的股票？  \n",
    "（3）选出合适的股票后如何构建投资组合并动态调整？  \n",
    "\n",
    "总体思维：多层次多角度分析  \n",
    "多角度保证在市场大方向上看对的正确率尽可能增加，多层次可以和多角度相互验证，获取超额收益。  \n",
    "通过自上而下的分析框架确定投资方向，选择符合投资方向的最优标的。  \n",
    "\n",
    "操作思路：根据股票基本面指标进行排序，通过设置参数和过滤值筛选投资标的，具体指标包括市盈率、市净率、流通股本、总市值、每股公积金、每股收益、收入同比、利润同比、毛利率、净利润率等。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据获取与分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-16T13:54:22.331404Z",
     "start_time": "2021-08-16T13:54:21.627234Z"
    }
   },
   "outputs": [],
   "source": [
    "#先引入后面可能用到的包（package）\n",
    "import pandas as pd  \n",
    "import tushare as ts \n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline   \n",
    "\n",
    "#正常显示画图时出现的中文和负号\n",
    "from pylab import mpl\n",
    "mpl.rcParams['font.sans-serif']=['SimHei']\n",
    "mpl.rcParams['axes.unicode_minus']=False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据获取\n",
    "基于tushare开源包获取A股基本面和交易数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-16T13:54:22.347395Z",
     "start_time": "2021-08-16T13:54:22.334394Z"
    }
   },
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "token='f6b511d8d4529f19319e1861edadda749e64a5b8573102deec80cfd8'\n",
    "ts.set_token(token)\n",
    "pro=ts.pro_api(token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-16T13:54:22.409011Z",
     "start_time": "2021-08-16T13:54:22.393398Z"
    }
   },
   "outputs": [],
   "source": [
    "#获取最新交易日期\n",
    "from dateutil.parser import parse\n",
    "from datetime import datetime,timedelta\n",
    "def get_cals():\n",
    "        #获取交易日历\n",
    "        cals=pro.trade_cal(exchange='')\n",
    "        cals=cals[cals.is_open==1].cal_date.values\n",
    "        return cals\n",
    "\n",
    "def get_trade_date():\n",
    "    #获取当天日期时间\n",
    "    d0=datetime.now()\n",
    "    if d0.hour>15:\n",
    "        d=d0.strftime('%Y%m%d')\n",
    "    else:\n",
    "        d=(d0-timedelta(1)).strftime('%Y%m%d')\n",
    "    while d not in get_cals():\n",
    "        d1=parse(d)\n",
    "        d=(d1-timedelta(1)).strftime('%Y%m%d')\n",
    "    return d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-16T13:54:24.916111Z",
     "start_time": "2021-08-16T13:54:22.555050Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>close</th>\n",
       "      <th>turnover_rate</th>\n",
       "      <th>turnover_rate_f</th>\n",
       "      <th>volume_ratio</th>\n",
       "      <th>pe</th>\n",
       "      <th>pe_ttm</th>\n",
       "      <th>pb</th>\n",
       "      <th>ps</th>\n",
       "      <th>ps_ttm</th>\n",
       "      <th>dv_ratio</th>\n",
       "      <th>dv_ttm</th>\n",
       "      <th>total_share</th>\n",
       "      <th>float_share</th>\n",
       "      <th>free_share</th>\n",
       "      <th>total_mv</th>\n",
       "      <th>circ_mv</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>002759.SZ</td>\n",
       "      <td>20210816</td>\n",
       "      <td>37.98</td>\n",
       "      <td>6.2503</td>\n",
       "      <td>12.1690</td>\n",
       "      <td>0.86</td>\n",
       "      <td>NaN</td>\n",
       "      <td>67.0517</td>\n",
       "      <td>5.3166</td>\n",
       "      <td>20.5616</td>\n",
       "      <td>12.4867</td>\n",
       "      <td>0.1053</td>\n",
       "      <td>NaN</td>\n",
       "      <td>40215.2567</td>\n",
       "      <td>40210.9817</td>\n",
       "      <td>20653.4623</td>\n",
       "      <td>1.527375e+06</td>\n",
       "      <td>1.527213e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000716.SZ</td>\n",
       "      <td>20210816</td>\n",
       "      <td>3.18</td>\n",
       "      <td>1.6672</td>\n",
       "      <td>1.7728</td>\n",
       "      <td>1.27</td>\n",
       "      <td>259.6830</td>\n",
       "      <td>50.6602</td>\n",
       "      <td>0.8643</td>\n",
       "      <td>0.6160</td>\n",
       "      <td>0.5746</td>\n",
       "      <td>0.6289</td>\n",
       "      <td>0.6289</td>\n",
       "      <td>74399.9550</td>\n",
       "      <td>69592.6611</td>\n",
       "      <td>65445.5803</td>\n",
       "      <td>2.365919e+05</td>\n",
       "      <td>2.213047e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>603519.SH</td>\n",
       "      <td>20210816</td>\n",
       "      <td>14.06</td>\n",
       "      <td>1.7725</td>\n",
       "      <td>4.1685</td>\n",
       "      <td>1.20</td>\n",
       "      <td>39.1509</td>\n",
       "      <td>36.2487</td>\n",
       "      <td>4.9881</td>\n",
       "      <td>2.8828</td>\n",
       "      <td>2.5998</td>\n",
       "      <td>1.7781</td>\n",
       "      <td>1.1854</td>\n",
       "      <td>26632.7839</td>\n",
       "      <td>26632.7839</td>\n",
       "      <td>11324.5567</td>\n",
       "      <td>3.744569e+05</td>\n",
       "      <td>3.744569e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>300925.SZ</td>\n",
       "      <td>20210816</td>\n",
       "      <td>27.27</td>\n",
       "      <td>19.3094</td>\n",
       "      <td>19.3094</td>\n",
       "      <td>1.24</td>\n",
       "      <td>49.3837</td>\n",
       "      <td>49.0545</td>\n",
       "      <td>5.3258</td>\n",
       "      <td>3.0816</td>\n",
       "      <td>2.6893</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.2157</td>\n",
       "      <td>22009.9166</td>\n",
       "      <td>5502.9000</td>\n",
       "      <td>5502.9000</td>\n",
       "      <td>6.002104e+05</td>\n",
       "      <td>1.500641e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>300153.SZ</td>\n",
       "      <td>20210816</td>\n",
       "      <td>16.85</td>\n",
       "      <td>27.5846</td>\n",
       "      <td>49.5377</td>\n",
       "      <td>1.25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.0711</td>\n",
       "      <td>7.6795</td>\n",
       "      <td>6.7669</td>\n",
       "      <td>0.1780</td>\n",
       "      <td>NaN</td>\n",
       "      <td>32000.0000</td>\n",
       "      <td>31734.7386</td>\n",
       "      <td>17671.1686</td>\n",
       "      <td>5.392000e+05</td>\n",
       "      <td>5.347303e+05</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code trade_date  close  turnover_rate  turnover_rate_f  volume_ratio  \\\n",
       "0  002759.SZ   20210816  37.98         6.2503          12.1690          0.86   \n",
       "1  000716.SZ   20210816   3.18         1.6672           1.7728          1.27   \n",
       "2  603519.SH   20210816  14.06         1.7725           4.1685          1.20   \n",
       "3  300925.SZ   20210816  27.27        19.3094          19.3094          1.24   \n",
       "4  300153.SZ   20210816  16.85        27.5846          49.5377          1.25   \n",
       "\n",
       "         pe   pe_ttm      pb       ps   ps_ttm  dv_ratio  dv_ttm  total_share  \\\n",
       "0       NaN  67.0517  5.3166  20.5616  12.4867    0.1053     NaN   40215.2567   \n",
       "1  259.6830  50.6602  0.8643   0.6160   0.5746    0.6289  0.6289   74399.9550   \n",
       "2   39.1509  36.2487  4.9881   2.8828   2.5998    1.7781  1.1854   26632.7839   \n",
       "3   49.3837  49.0545  5.3258   3.0816   2.6893       NaN  0.2157   22009.9166   \n",
       "4       NaN      NaN  7.0711   7.6795   6.7669    0.1780     NaN   32000.0000   \n",
       "\n",
       "   float_share  free_share      total_mv       circ_mv  \n",
       "0   40210.9817  20653.4623  1.527375e+06  1.527213e+06  \n",
       "1   69592.6611  65445.5803  2.365919e+05  2.213047e+05  \n",
       "2   26632.7839  11324.5567  3.744569e+05  3.744569e+05  \n",
       "3    5502.9000   5502.9000  6.002104e+05  1.500641e+05  \n",
       "4   31734.7386  17671.1686  5.392000e+05  5.347303e+05  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date=get_trade_date()\n",
    "data=pro.daily_basic(trade_date=date)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ts_code\tstr\tTS股票代码\n",
    "trade_date\tstr\t交易日期\n",
    "close\tfloat\t当日收盘价\n",
    "turnover_rate\tfloat\t换手率（%）\n",
    "turnover_rate_f\tfloat\t换手率（自由流通股）\n",
    "volume_ratio\tfloat\t量比\n",
    "pe\tfloat\t市盈率（总市值/净利润， 亏损的PE为空）\n",
    "pe_ttm\tfloat\t市盈率（TTM，亏损的PE为空）\n",
    "pb\tfloat\t市净率（总市值/净资产）\n",
    "ps\tfloat\t市销率\n",
    "ps_ttm\tfloat\t市销率（TTM）\n",
    "dv_ratio\tfloat\t股息率 （%）\n",
    "dv_ttm\tfloat\t股息率（TTM）（%）\n",
    "total_share\tfloat\t总股本 （万股）\n",
    "float_share\tfloat\t流通股本 （万股）\n",
    "free_share\tfloat\t自由流通股本 （万）\n",
    "total_mv\tfloat\t总市值 （万元）\n",
    "circ_mv\tfloat\t流通市值（万元）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据描述性统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-16T13:54:24.963122Z",
     "start_time": "2021-08-16T13:54:24.918113Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>close</th>\n",
       "      <th>turnover_rate</th>\n",
       "      <th>turnover_rate_f</th>\n",
       "      <th>volume_ratio</th>\n",
       "      <th>pe</th>\n",
       "      <th>pe_ttm</th>\n",
       "      <th>pb</th>\n",
       "      <th>ps</th>\n",
       "      <th>ps_ttm</th>\n",
       "      <th>dv_ratio</th>\n",
       "      <th>dv_ttm</th>\n",
       "      <th>total_share</th>\n",
       "      <th>float_share</th>\n",
       "      <th>free_share</th>\n",
       "      <th>total_mv</th>\n",
       "      <th>circ_mv</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>4435.00</td>\n",
       "      <td>4435.00</td>\n",
       "      <td>4435.00</td>\n",
       "      <td>4422.00</td>\n",
       "      <td>3835.00</td>\n",
       "      <td>3823.00</td>\n",
       "      <td>4395.00</td>\n",
       "      <td>4434.00</td>\n",
       "      <td>4431.00</td>\n",
       "      <td>2630.00</td>\n",
       "      <td>2761.00</td>\n",
       "      <td>4435.00</td>\n",
       "      <td>4435.00</td>\n",
       "      <td>4435.00</td>\n",
       "      <td>4.435000e+03</td>\n",
       "      <td>4.435000e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>26.83</td>\n",
       "      <td>3.52</td>\n",
       "      <td>4.93</td>\n",
       "      <td>1.08</td>\n",
       "      <td>81.38</td>\n",
       "      <td>72.05</td>\n",
       "      <td>5.28</td>\n",
       "      <td>33.64</td>\n",
       "      <td>46.53</td>\n",
       "      <td>1.39</td>\n",
       "      <td>1.49</td>\n",
       "      <td>171431.61</td>\n",
       "      <td>131280.56</td>\n",
       "      <td>64662.55</td>\n",
       "      <td>2.097254e+06</td>\n",
       "      <td>1.571759e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>58.32</td>\n",
       "      <td>5.00</td>\n",
       "      <td>6.27</td>\n",
       "      <td>0.65</td>\n",
       "      <td>159.77</td>\n",
       "      <td>236.01</td>\n",
       "      <td>20.08</td>\n",
       "      <td>1278.77</td>\n",
       "      <td>1872.06</td>\n",
       "      <td>1.71</td>\n",
       "      <td>1.72</td>\n",
       "      <td>1064175.05</td>\n",
       "      <td>775500.80</td>\n",
       "      <td>186915.92</td>\n",
       "      <td>7.590400e+06</td>\n",
       "      <td>6.108980e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.91</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.08</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.80</td>\n",
       "      <td>0.20</td>\n",
       "      <td>0.36</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.01</td>\n",
       "      <td>3700.00</td>\n",
       "      <td>751.71</td>\n",
       "      <td>746.99</td>\n",
       "      <td>7.173585e+04</td>\n",
       "      <td>3.609593e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>6.30</td>\n",
       "      <td>0.81</td>\n",
       "      <td>1.42</td>\n",
       "      <td>0.73</td>\n",
       "      <td>22.92</td>\n",
       "      <td>19.90</td>\n",
       "      <td>1.78</td>\n",
       "      <td>1.72</td>\n",
       "      <td>1.60</td>\n",
       "      <td>0.36</td>\n",
       "      <td>0.42</td>\n",
       "      <td>23517.41</td>\n",
       "      <td>15905.00</td>\n",
       "      <td>9197.23</td>\n",
       "      <td>3.352217e+05</td>\n",
       "      <td>2.155002e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>12.18</td>\n",
       "      <td>1.84</td>\n",
       "      <td>2.91</td>\n",
       "      <td>0.92</td>\n",
       "      <td>40.57</td>\n",
       "      <td>35.01</td>\n",
       "      <td>2.94</td>\n",
       "      <td>3.69</td>\n",
       "      <td>3.41</td>\n",
       "      <td>0.81</td>\n",
       "      <td>0.94</td>\n",
       "      <td>51990.50</td>\n",
       "      <td>43531.00</td>\n",
       "      <td>25245.76</td>\n",
       "      <td>5.926215e+05</td>\n",
       "      <td>4.268531e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>26.14</td>\n",
       "      <td>4.06</td>\n",
       "      <td>5.97</td>\n",
       "      <td>1.23</td>\n",
       "      <td>78.73</td>\n",
       "      <td>63.43</td>\n",
       "      <td>4.97</td>\n",
       "      <td>7.52</td>\n",
       "      <td>6.94</td>\n",
       "      <td>1.71</td>\n",
       "      <td>1.92</td>\n",
       "      <td>114733.75</td>\n",
       "      <td>97506.82</td>\n",
       "      <td>58761.52</td>\n",
       "      <td>1.467731e+06</td>\n",
       "      <td>1.089930e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1699.20</td>\n",
       "      <td>71.07</td>\n",
       "      <td>141.99</td>\n",
       "      <td>15.45</td>\n",
       "      <td>4434.10</td>\n",
       "      <td>7036.15</td>\n",
       "      <td>1041.09</td>\n",
       "      <td>81955.83</td>\n",
       "      <td>109274.32</td>\n",
       "      <td>27.03</td>\n",
       "      <td>23.79</td>\n",
       "      <td>35640625.71</td>\n",
       "      <td>29405529.39</td>\n",
       "      <td>5041233.49</td>\n",
       "      <td>2.134531e+08</td>\n",
       "      <td>2.134531e+08</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         close  turnover_rate  turnover_rate_f  volume_ratio       pe  \\\n",
       "count  4435.00        4435.00          4435.00       4422.00  3835.00   \n",
       "mean     26.83           3.52             4.93          1.08    81.38   \n",
       "std      58.32           5.00             6.27          0.65   159.77   \n",
       "min       0.91           0.03             0.08          0.04     0.80   \n",
       "25%       6.30           0.81             1.42          0.73    22.92   \n",
       "50%      12.18           1.84             2.91          0.92    40.57   \n",
       "75%      26.14           4.06             5.97          1.23    78.73   \n",
       "max    1699.20          71.07           141.99         15.45  4434.10   \n",
       "\n",
       "        pe_ttm       pb        ps     ps_ttm  dv_ratio   dv_ttm  total_share  \\\n",
       "count  3823.00  4395.00   4434.00    4431.00   2630.00  2761.00      4435.00   \n",
       "mean     72.05     5.28     33.64      46.53      1.39     1.49    171431.61   \n",
       "std     236.01    20.08   1278.77    1872.06      1.71     1.72   1064175.05   \n",
       "min       0.20     0.36      0.03       0.03      0.00     0.01      3700.00   \n",
       "25%      19.90     1.78      1.72       1.60      0.36     0.42     23517.41   \n",
       "50%      35.01     2.94      3.69       3.41      0.81     0.94     51990.50   \n",
       "75%      63.43     4.97      7.52       6.94      1.71     1.92    114733.75   \n",
       "max    7036.15  1041.09  81955.83  109274.32     27.03    23.79  35640625.71   \n",
       "\n",
       "       float_share  free_share      total_mv       circ_mv  \n",
       "count      4435.00     4435.00  4.435000e+03  4.435000e+03  \n",
       "mean     131280.56    64662.55  2.097254e+06  1.571759e+06  \n",
       "std      775500.80   186915.92  7.590400e+06  6.108980e+06  \n",
       "min         751.71      746.99  7.173585e+04  3.609593e+04  \n",
       "25%       15905.00     9197.23  3.352217e+05  2.155002e+05  \n",
       "50%       43531.00    25245.76  5.926215e+05  4.268531e+05  \n",
       "75%       97506.82    58761.52  1.467731e+06  1.089930e+06  \n",
       "max    29405529.39  5041233.49  2.134531e+08  2.134531e+08  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe().round(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 指标选股\n",
    "选股就是不断剔除股票，留下自己“中意”股票的过程。针对上述基本面和交易数据设置参数和过滤值，剔除掉大部分股票。注意，由于这里用到的基本面数据仅仅是2018年三季度的，企业的经营是动态演进的，市场交易频率更高，因而对股票的筛选也应该是一个动态的过程，这里只是给出一个静态的思路，以供参考，所选股票也不构成投资建议，切记！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-16T13:54:26.715817Z",
     "start_time": "2021-08-16T13:54:24.965123Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>close</th>\n",
       "      <th>pe_ttm</th>\n",
       "      <th>pb</th>\n",
       "      <th>ps_ttm</th>\n",
       "      <th>dv_ttm</th>\n",
       "      <th>total_share</th>\n",
       "      <th>turnover_rate</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ts_code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>600985.SH</th>\n",
       "      <td>12.81</td>\n",
       "      <td>8.2901</td>\n",
       "      <td>1.4667</td>\n",
       "      <td>0.5917</td>\n",
       "      <td>4.6762</td>\n",
       "      <td>248103.5925</td>\n",
       "      <td>3.5557</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           close  pe_ttm      pb  ps_ttm  dv_ttm  total_share  turnover_rate\n",
       "ts_code                                                                     \n",
       "600985.SH  12.81  8.2901  1.4667  0.5917  4.6762  248103.5925         3.5557"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#设置参数和过滤值(根据需要不断调整)\n",
    "#市盈率>0\n",
    "date=get_trade_date()\n",
    "data=pro.daily_basic(trade_date=date)\n",
    "close=data['close']<20\n",
    "pe0=data['pe']<30\n",
    "pe1=data['pe_ttm']<30\n",
    "#市净率>0\n",
    "pb0=data['pb']>0\n",
    "pb1=data['pb']<3\n",
    "#市销率\n",
    "ps=data['ps']<6\n",
    "\n",
    "#流通股本<=300亿 \n",
    "out = data['circ_mv'] <= 3000000\n",
    "tr=data['turnover_rate']>3\n",
    "#股息率\n",
    "dv=data['dv_ttm']>4\n",
    "#取并集结果:\n",
    "select = close &pe0 &pe1  &pb0 & pb1 & ps &out&tr&dv\n",
    "data= data[select] \n",
    "data.index=data.ts_code\n",
    "data=data[['close','pe_ttm','pb','ps_ttm','dv_ttm','total_share','turnover_rate']]\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
